package cn.oa.service.impl;

import cn.oa.dao.AttendanceDao;
import cn.oa.entity.Attendance;
import cn.oa.entity.AttendanceVo;
import cn.oa.entity.Employee;
import cn.oa.service.AttendanceService;
import cn.oa.util.DateUtil;
import cn.oa.util.Myutil;
import cn.oa.util.Page;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/29 9:05
 **/
public class AttendanceServiceImpl implements AttendanceService {
	@Override
	public int addSignInByEmployeeId(int employeeId, String date) throws SQLException {
		AttendanceDao attendanceDao = null;
		SqlSession sqlSession = null;
		Attendance attendance = null;
		int ret = 0;
		try {
			String resource = "mybatis-config.xml";
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = factory.openSession();

			attendanceDao = sqlSession.getMapper(AttendanceDao.class);
			attendance = attendanceDao.getRecordByEmployeeIdAndDate(employeeId, date);

			if (attendance == null) {
				ret = attendanceDao.addSinginByEmployeeId(employeeId);
			} else {
				ret = 0;
			}

			sqlSession.commit();
		} catch ( IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}

		return ret;
	}

	@Override
	public int addSignOutByEmployeeId(int employeeId, String date) throws SQLException {
		int result = 0;
		try{
			SqlSession sqlSession= Myutil.getSqlSession();
			AttendanceDao dao=sqlSession.getMapper(AttendanceDao.class);
			String now = DateUtil.now();
			//查询员工是否有上班打卡记录
			Attendance record = dao.getRecordByEmployeeIdAndDate(employeeId, now);
			if(record==null){
				//没有上班打卡记录时不允许执行下班打卡操作
				result = 0;
			}else{
				if(record.getSignOutTime()==null){
					//执行下班打卡
					result = dao.updateSingoutByAttendanceId(record.getId());
				}else{
					result = -2;
				}
			}
			sqlSession.commit();
			sqlSession.close();
		}catch(SQLException e){
			e.printStackTrace();
//			throw e;
		}
		return result;
	}

	@Override
	public void getAttendancesByPage(String name, String searchDate, Page pageObj) throws SQLException {
		SqlSession sqlSession=Myutil.getSqlSession();
		try {
			AttendanceDao attendanceDao = sqlSession.getMapper(AttendanceDao.class);

			AttendanceVo attendanceVo=new AttendanceVo(name,searchDate,pageObj.getCurrPageNo(),pageObj.getPageSize());
			int totalCount = attendanceDao.getAttendancesCount(attendanceVo);
			pageObj.setTotalCount(totalCount);
			if (totalCount > 0) {
				if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
					pageObj.setCurrPageNo(pageObj.getTotalPageCount());
				}
				List<Attendance> attendanceList = attendanceDao.getAttendancesByPage(attendanceVo);
				pageObj.setAttendanceList(attendanceList);
			} else {
				pageObj.setCurrPageNo(0);
				pageObj.setAttendanceList(new ArrayList<Attendance>());
			}

		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}finally{
			sqlSession.close();
		}
	}
	}

